using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

namespace TTDotNet.Engine.GUI.Windows
{
    public class Window
    {
        protected RectangleF screenRectangle;
        protected SizeF minimumSize;
        protected Dictionary<int, Control> windowControls;
        protected bool isHidden;
        private bool alwaysOnTop;

        public Window()
        {
            windowControls = new Dictionary<int, Control>();
            screenRectangle = new RectangleF();
            minimumSize = new SizeF(float.MinValue, float.MinValue);
        }

        public RectangleF Area
        {
            get { return screenRectangle; }
        }

        public SizeF MinimumSize
        {
            get { return minimumSize; }
            set { minimumSize = value; }
        }

        public virtual void ResizeByFactor(float widthScale, float heightScale) 
        {

        }

        public virtual bool ResizeTo(float width, float height) 
        {
            float widthFactor = width / screenRectangle.Width;
            float heightFactor = height / screenRectangle.Height;

            if (width < MinimumSize.Width) { widthFactor = 1; }
            if (height < MinimumSize.Height) { heightFactor = 1; }

            foreach (Control c in windowControls.Values)
            {
                c.ResizeBy(widthFactor, heightFactor);
            }

            screenRectangle.Width = width;
            screenRectangle.Height = height;

            return true;
        }

        public virtual void MoveBy(float right, float down)
        {
            screenRectangle.X += right;
            screenRectangle.Y += down;
        }

        protected void SetScreenRectangle(RectangleF rect)
        {
            this.screenRectangle = rect;
        }

        public bool Contains(Point cursor)
        {
            return screenRectangle.Contains(cursor);
        }

        public virtual void Render()
        {
            if (!isHidden)
            {
                foreach (Control c in windowControls.Values)
                {
                    c.Render(screenRectangle.Location);
                }
            }
        }

        public bool MouseHandler(Point cursor, bool[] buttons, float zDelta)
        {
            bool result = false;

            if (Contains(cursor))
            {
                foreach (Control c in windowControls.Values)
                {
                    result = c.MouseHandler(Point.Subtract(cursor, new Size((int)screenRectangle.X, (int)screenRectangle.Y)), buttons, zDelta) || result;
                }
                return true;
            }
            else
            {
                foreach (Control c in windowControls.Values)
                {
                    c.MouseHandler(Point.Subtract(cursor, new Size((int)screenRectangle.X, (int)screenRectangle.Y)), buttons, zDelta);
                }
                return false;
            }
        }

        public virtual void Close()
        {
            foreach (Control c in windowControls.Values)
            {
                c.Dispose();
            }
            //windowControls.Clear();

            Game.newGUI.CloseWindow(this);
        }

        public void Hide()
        {
            isHidden = true;
        }

        public void Show()
        {
            isHidden = false;
        }

        public virtual void Update()
        {

        }

        public bool AlwaysOnTop
        {
            get { return alwaysOnTop; }
            set { alwaysOnTop = value; }
        }
    }
}
